package com.panopset.gui;

import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import com.panopset.java.Version;

/**
 * Panopset list editor.
 *
 * @author Karl Dinwiddie
 *
 */
public final class ListEditor extends AbstractTableEditor {

    @Override
    protected void doAdd() {
        getModel().addRow(new String[] {"" });
    }

    @Override
    protected DefaultTableModel createModel() {
        return new DefaultTableModel(0, 1) {
            @Override
            public String getColumnName(final int columnIndex) {
                switch (columnIndex) {
                case 0:
                    return getValueColumnHeader();
                default:
                    return "undefined";
                }
            }

            /**
             * Serial version id.
             */
            private static final long serialVersionUID = Version.UNI_VERSION;
        };
    }

    protected void load(final Vector<String> v) {
        getModel().getDataVector().clear();
        for (String s : v) {
            getModel().addRow(new String[] {s });
        }
    }

    protected Vector<String> save() {
        Vector<String> rtn = new Vector<String>();
        for (Object o : getModel().getDataVector()) {
            if (o != null && o instanceof Vector<?>) {
                Vector<?> v = (Vector<?>) o;
                if (!v.isEmpty()) {
                    for (Object s : v) {
                        rtn.add(s.toString());
                    }
                }
            }
        }
        return rtn;
    }
}
